<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 20;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$this->_template_f = $this->config->item('template_f');
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('menu/menu_model');
		$this->load->language('faqs/faqs', $this->_lang);
	}
	
	function index()
	{
		$page = (int)$this->input->get('page',true);
		if($page < 1) $page = 1;
		$cat = (int)$this->input->get('cat',true);
		
		$data = array();
		$data['lang'] = $this->lang->line('faqs');
		$data['lstCat'] = $this->menu_model->getall_menu();
		$data['cat'] = $cat;
		$topics = $this->menu_model->get_topics($cat,0,$page,$this->_numItem);
		$data['topics'] = $topics['items'];
		$data['page'] = $page;
		$data['pages'] = $topics['pages'];
		$data['stt'] = ($page - 1)*$this->_numItem;

		$this->_loadHeader($data['lang']['faqs']);
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'menu/index', $data);
		
		$this->_loadFooter();
	}
	
	function Categories()
	{
		$data = array();
		$data['lang'] = $this->lang->line('faqs');
		$data['lstCat'] = $this->faqs_model->getall_categories();

		$this->_loadHeader($data['lang']['faqs']);
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'faqs/category_index', $data);
		
		$this->_loadFooter();
	}
	
	function addCategory()
	{
		$data = array();
		$data['lang'] = $this->lang->line('faqs');
		$data['lstCat'] = $this->faqs_model->getall_categories();

		$this->_loadHeader($data['lang']['faqs']);
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'faqs/category_add', $data);
		
		$this->_loadFooter();
	}
	
	function insertCategory()
	{
		if(isset($_POST['catname']) && $_POST['catname'] != '')
		{
			$parent = (int)$this->input->post('level',true);
			$name = trim($this->input->post('catname',true));
			$this->faqs_model->insert_category($name, $parent);
		}
		redirect(site_url('faqs/Categories'));
	}
	
	function updateCategoryName()
	{
		if(isset($_POST['id']) && (int)$_POST['id'] > 0 && trim($_POST['text']) != '')
		{
			$id = (int)$this->input->post('id', true);
			$text = trim($this->input->post('text', true));
			
			$this->faqs_model->updateCategoryName($id, $text);
			echo '1'; die();
		}
		
		$lang = $this->lang->line('faqs');
		echo $lang['please_try_again'];
	}
	
	function getTopic()
	{
		$parent = (int)$this->input->get('parent',true);
		$topics = $this->faqs_model->get_topics(0,$parent,1,$this->_numItem);
		$votes = $this->faqs_model->getTopicVote($parent);
		$data = array();
		$data['lang'] = $this->lang->line('faqs');
		$data['items'] = $topics['items'];
		$data['votes'] = $votes;
		$this->load->view($this->_template_f.'faqs/answer', $data);
	}
	
	function addTopic()
	{
		if(isset($_POST['content']))
		{
			$content = $this->input->post('content', true);
			$parent = (int)$this->input->post('parent_id', true);
			$cat_id = (int)$this->input->post('cat_id', true);
			$id = (int)$this->input->post('id', true);
			if($id > 0)
				$this->faqs_model->update_topic($id, $content, $cat_id, $parent);
			else
				$this->faqs_model->insert_topic($content, $cat_id, $parent);
			
			redirect(site_url('faqs')); die();
		}
		
		$data = array();
		
		$data['id'] = (int)$this->input->get('id', true);
		$data['content'] = '';
		$data['cat_id'] = 0;
		$data['parent_id'] = 0;
		if($data['id'] > 0)
		{
			$item = $this->faqs_model->get_topic_info($data['id']);
			$data['content'] = $item->content;
			$data['cat_id'] = $item->cat_id;
			$data['parent_id'] = $item->parent_id;
			//$data['topics'] = $this->faqs_model->get_topics($data['cat_id']);
		}
		$data['topics'] = $this->faqs_model->get_topics(0,0,1,10000000);
		
		$data['categories'] = $this->faqs_model->getall_categories();
		
		$data['lang'] = $lang = $this->lang->line('faqs');
		
		$this->_loadHeader($data['lang']['faqs']);
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'faqs/add_topic', $data);
		
		$this->_loadFooter();
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
}